package com.apache.gateway.log;

import com.apache.method.SystemRunnable;
import com.apache.tools.OsUtils;
import com.apache.tools.StrUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * @description: 日志写入线程
 * @author: Hou Dayu
 * @date: Created in 2020/9/8
 */
public class LogWritRunable extends SystemRunnable {

    private String message;
    private String levelStr;

    public LogWritRunable(String message, String levelStr){
        this.message = message;
        this.levelStr = levelStr;
    }

    @Override
    public void run() {
        try {
            Map<String,Object> object = new HashMap();
            object.put("ipAddress", OsUtils.getLocalIp());
            String str[] = message.split(";");
            for (int i = 0; i < str.length; i++) {
                String vals[] = str[i].split(" = ");
                if (vals.length > 1 && StrUtil.isNotNull(vals[1])) {
                    if ("errorMessage".equals(vals[0].trim())) {
                        object.put(vals[0].trim(), vals[1].replace("{", "").replace("}", ""));
                    } else {
                        object.put(vals[0].trim(), vals[1]);
                    }
                }
            }
            OpcLogQueue.getInstance().add(object);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
